//
//  CycleWaveView.m
//  iOSHeartAnimation
//
//  Created by ddy on 2018/8/9.
//  Copyright © 2018年 ddy. All rights reserved.
//

#import "CycleWaveView.h"
#define CycleWaveRGBA(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
#define Cycle_WaveTotallHeight 60.0
@implementation CycleWaveView
{
    CADisplayLink *_c_link;
    CGFloat _c_offset;
    CAShapeLayer *_c_layer1;
    CAShapeLayer *_c_layer2;
    CGFloat _c_waveWidth;
    UIColor *_firstFillColor;
    UIColor *_secondFillColor;
    //h ＝ 30；也就是说，当Asin（ωx+φ）计算为0的时候，这个时候y的位置是30；
    CGFloat _c_wave_h;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self c_initLayerAndProperty];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self c_initLayerAndProperty];
    }
    return self;
}

/**
 * 初始化控件
 */
- (void)c_initLayerAndProperty {
    _firstFillColor = nil;
    _secondFillColor = nil;
    _c_waveWidth = self.frame.size.width;
    _c_wave_h    = 30.0;
    
    _c_layer1 = [CAShapeLayer layer];
    _c_layer1.opacity = 0.3;
    _c_layer1.frame = self.bounds;
    
    _c_layer2 = [CAShapeLayer layer];
    _c_layer2.frame = self.bounds;
    _c_layer2.opacity = 0.3;
    
    [self.layer addSublayer:_c_layer1];
    [self.layer addSublayer:_c_layer2];
}

/**
 * 开始wave
 */
- (void)c_startWaving{
    _c_link = [CADisplayLink displayLinkWithTarget:self selector:@selector(startAnimation)];
    [_c_link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)startAnimation{
    _c_offset += _c_speed;
   // NSLog(@"%f",_c_offset);
    ///整体高度
    if (self.c_waveTotallHeight == 0.0) {
        self.c_waveTotallHeight = Cycle_WaveTotallHeight;
    }
    ///第一波浪
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGFloat startY = _c_waveHeight *sinf(_c_offset*M_PI/_c_waveWidth);
    CGPathMoveToPoint(pathRef, NULL, 0, startY);
    for (CGFloat i = 0.0;  i < _c_waveWidth ; i ++ ) {
        CGFloat y = 1.1 * _c_waveHeight * sinf(2.5*M_PI*i/_c_waveWidth + _c_offset*M_PI/_c_waveWidth) + _c_wave_h;
        CGPathAddLineToPoint(pathRef, NULL, i, y);
    }
    
    CGPathAddLineToPoint(pathRef, NULL, _c_waveWidth, self.c_waveTotallHeight);
    CGPathAddLineToPoint(pathRef, NULL,0, self.c_waveTotallHeight);
    CGPathCloseSubpath(pathRef);
    
    _c_layer1.path = pathRef;
    if (!_firstFillColor) {
        _c_layer1.fillColor = CycleWaveRGBA(201, 229, 241, 1).CGColor;
    }
    CGPathRelease(pathRef);
    
    ///第二波浪
    CGMutablePathRef pathRef2 = CGPathCreateMutable();
    CGFloat startY2 = _c_waveHeight * sinf(_c_offset * M_PI / _c_waveWidth + M_PI/4);
    CGPathMoveToPoint(pathRef2, NULL, 0, startY2);
    for (CGFloat i = 0.0;  i < _c_waveWidth; i++) {
        CGFloat y = _c_waveHeight * sinf(2.5 * M_PI * i / _c_waveWidth + 3 * _c_offset * M_PI / _c_waveWidth + M_PI/4) + _c_wave_h;
        CGPathAddLineToPoint(pathRef2, NULL, i, y);
    }
    CGPathAddLineToPoint(pathRef2, NULL, _c_waveWidth, self.c_waveTotallHeight);
    CGPathAddLineToPoint(pathRef2, NULL, 0, self.c_waveTotallHeight);
    CGPathCloseSubpath(pathRef2);
    _c_layer2.path = pathRef2;
    if (!_secondFillColor) {
        _c_layer2.fillColor = CycleWaveRGBA(0, 152, 215, 1).CGColor;
    }
    CGPathRelease(pathRef2);
}
/**
 * 结束waving
 */
- (void)c_stopWave{
    [_c_link invalidate];
    _c_link = nil;
}

/**
 * 设置波浪颜色和透明度
 */
- (void)c_setfirstWaveColor:(UIColor *)firstWaveColor firstWaveAlpha:(CGFloat)alpha1 secondWaveColor:(UIColor *)secondWaveColor secondWaveAlpha:(CGFloat)alpha2{
    //颜色
    _c_layer1.fillColor = firstWaveColor.CGColor;
    _c_layer2.fillColor = secondWaveColor.CGColor;
    _firstFillColor  = firstWaveColor;
    _secondFillColor = secondWaveColor;
    //透明度
    _c_layer1.opacity = alpha1;
    _c_layer2.opacity = alpha2;
}
@end
